<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\admin\model\Log;
use app\admin\model\User;
use app\BaseController;
use think\annotation\Route;
use think\exception\ValidateException;
use think\facade\View;

class Login extends BaseController
{
    public function index()
    {
        $url = input('url');
        View::assign('url',$url);
        return view();
    }

    public function check()
    {
        $data = input();

        $validate = new \app\admin\validate\Login;
        if (!$validate->check($data)) {
            return msg_error($validate->getError());
        }

        $where = [
            'username'=>$data['username'],
            'password'=>encrypt($data['password'],$data['username']),
        ];

        $user = User::where($where)->find();

        if($user){
            session('token',encrypt($user->name));
            $user->last_login_time = now();
            $user->save();
            Log::create([
                'user'=>$user->name,
                'value'=>'登陆',
                'info'=>'',
            ]);
            return msg_success('登陆成功');
        }else{
            return msg_error('密码错误');
        }
    }
    public function logout()
    {
        session('token',false);
        return msg_success();
    }
    public function message()
    {
        $msg = input('msg','异常');
        return view('message',['msg'=>$msg]);
    }
}
